<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>

</head>

<body>
    <div class="bookContainer">
        <h2>图书列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script>
            getBookList();
            function getBookList() {
                // 从后端获得数据
                $.ajax({
                    url: "/book/getBookListByPage" + location.search,//location.search获取url中的参数,如：?currentPage=1,包含?号
                    type: "GET",
                    success: function (result) {
                        // 返回的数据result，是Result<PageResult<BookInfo>>类型，我们要对里面的data属性进行遍历
                        //使用for循环进行字符串拼接
                        //    if(result.code == "NOLOGIN"){
                        //         alert("请先登录");
                        //         location.href = "login.html";
                        //         return;
                        //     }
                        if (result.data.records == null || result.data == null) {
                            alert("没有图书数据");
                            return;
                        }
                        var bookList = result.data.records;

                        var html = "";

                        for (var i = 0; i < bookList.length; i++) {
                            var book = bookList[i];
                            html += "<tr>";
                            html += "<td><input type='checkbox' name='selectBook' value='" + book.id + "' id='selectBook' class='book-select'></td>";
                            html += "<td>" + book.id + "</td>";
                            html += "<td>" + book.bookName + "</td>";
                            html += "<td>" + book.author + "</td>";
                            html += "<td>" + book.count + "</td>";
                            html += "<td>" + book.price + "</td>";
                            html += "<td>" + book.publish + "</td>";
                            html += "<td>" + book.statusCN + "</td>";
                            html += "<td><div class='op'></div>";
                            html += "<a href='book_update.html?bookId=" + book.id + "'>修改 </a>";
                            html += "<a href='javascript:void(0)' onclick='deleteBook(" + book.id + ")'>删除</a></td>";
                            html += "</tr>";

                        }
                        //将html填充到表格中
                        $("tbody").html(html);

                        //翻页信息
                        $("#pageContainer").jqPaginator({
                            totalCounts: result.data.count, //总记录数,也就是页面底部显示的总页数
                            pageSize: 10,    //每页的记录数
                            visiblePages: 5, //可视页数
                            currentPage: result.data.pageRequest.currentPage,  //当前页码
                            first: '<li class="page-item"><a class="page-link">首页</a></li>',
                            prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                            next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                            last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                            page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                            //页面初始化和页码点击时都会执行
                            onPageChange: function (page, type) {
                                console.log("第" + page + "页, 类型:" + type);
                                if (type == "change") {
                                    //跳转到指定页码
                                    location.href = "book_list.html?currentPage=" + page;//跳转到指定页码
                                }
                            }
                        });

                    },
                    error: function (error) {
                        console.log(error);
                        if (error != null && error.status == 401) {
                            location.href = "login.html";
                        }
                    }
                })
            }


            //删除图书
            function deleteBook(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    //删除图书
                    $.ajax({
                        url: "/book/deleteBook",
                        type: "post",
                        data: {
                            //冒号左边是后端方法中的形参，右边是前端方法中的参数
                            bookId: id
                        },
                        success: function (result) {
                            console.log(result);
                            if(result.code == "SUCCESS" && result.data == "") {
                                //刷新页面
                                location.href = "book_list.html";
                            } else {
                                alert("删除失败，请联系管理员");
                            }

                        },
                        error:function (error) {
                            //用户未登录
                            if(error != null && error.status == 401){
                                location.href = "login.html";
                            }
                        }
                    });
                }
            }
            //批量删除图书
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    //获取所有选中的复选框的id
                    $("input:checkbox[name='selectBook']:checked").each(function () {
                        ids.push($(this).val());
                    });
                    console.log(ids);
                    //删除图书
                    $.ajax({
                        //bookIds是后端方法中的形参，ids是前端方法中的参数，将ids作为参数传递给后端方法
                        url: "/book/batchDeleteBook?bookIds=" + ids,
                        type: "post",
                        success: function (result) {
                            if(result.code == "SUCCESS" && result.data == "") {
                                //刷新页面
                                alert("批量删除成功");
                                location.href = "book_list.html";
                            } else {
                                alert("批量删除失败，请联系管理员");
                            }
                        },
                        error:function (error) {
                            //用户未登录
                            if(error != null && error.status == 401){
                                location.href = "login.html";
                            }
                        }
                    })
                }
            }

        </script>
    </div>
</body>

</html>